package com.xiao.common.utils;

import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2014/10/29.
 */
public class FileUtil {

    public static void main(String[] args){
        //方式一
        System.out.println("path= "+System.getProperty("user.dir"));

        //方式二
        File directory = new File("");//设定为当前文件夹

        System.out.println("abs path = " + directory.getAbsoluteFile());
        try {
            System.out.println("can path = " + directory.getCanonicalPath());
        }catch (IOException e){

        }

        //方式三
        System.out.println("get res / : "+FileUtil.class.getResource("/"));
        System.out.println("get res :"+FileUtil.class.getResource(""));
        //方式4
        System.out.println("get loader res :"+FileUtil.class.getClassLoader().getResource(""));
        System.out.println("get loader res xml:"+FileUtil.class.getClassLoader().getResource("source.xml"));
    }

    /**
        获取根路径
     */
    public static String getRootPath(){
        return String.valueOf(FileUtil.class.getResource("/"));
    }

    /**
     * 获取根目录下的文件
     * @param fileName  文件名，需要全名，带后缀
     * @return
     */
    public static String getRootFilePathByName(String fileName){
        return String.valueOf(FileUtil.class.getClassLoader().getResource(fileName));
    }
}
